using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.Repositories
{
	#region using
	using AppManagement.Models;
	using AppManagement.Helpers;
	#endregion
	public class CHITIETPHIEUXUATKHORepository
	{
		public static List<CHITIETPHIEUXUATKHO> SelectAll()
		{
			try
			{
				return DataContext.Instance.CHITIETPHIEUXUATKHO.ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<CHITIETPHIEUXUATKHO>();
		}

		public static CHITIETPHIEUXUATKHO SelectByID(int chitietphieuxuatkho)
		{
			try
			{
				return DataContext.Instance.CHITIETPHIEUXUATKHO.FirstOrDefault(item => item.CHITIETPHIEUXUATKHO_ID == chitietphieuxuatkho );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public static bool Insert(CHITIETPHIEUXUATKHO obj)
		{
			try
			{
				DataContext.Instance.CHITIETPHIEUXUATKHO.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Delete(int chitietphieuxuatkho)
		{
			try
			{
				var delitem = DataContext.Instance.CHITIETPHIEUXUATKHO.FirstOrDefault(item => item.CHITIETPHIEUXUATKHO_ID == chitietphieuxuatkho );
				DataContext.Instance.CHITIETPHIEUXUATKHO.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static List<CHITIETPHIEUXUATKHO> RetrieveByID(int chitietphieuxuatkho)
		{
			try
			{
				return (from item in DataContext.Instance.CHITIETPHIEUXUATKHO where  item.CHITIETPHIEUXUATKHO_ID == chitietphieuxuatkho  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<CHITIETPHIEUXUATKHO>();
		}

		public static List<CHITIETPHIEUXUATKHO> SelectByPHIEU_XUAT_KHO_ID(int phieu_xuat_kho_id)
		{
			try
			{
			var lstItem = (from item in DataContext.Instance.CHITIETPHIEUXUATKHO where item.PHIEU_XUAT_KHO_ID == phieu_xuat_kho_id select item).ToList();
			return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<CHITIETPHIEUXUATKHO>();
		}

		public static List<CHITIETPHIEUXUATKHO> SelectByTHIET_BI_ID(int thiet_bi_id)
		{
			try
			{
			var lstItem = (from item in DataContext.Instance.CHITIETPHIEUXUATKHO where item.THIET_BI_ID == thiet_bi_id select item).ToList();
			return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<CHITIETPHIEUXUATKHO>();
		}

	}
}